package com.primeton.wh.mcenter.impl.message.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.primeton.wh.mcenter.api.model.Application;
import com.primeton.wh.mcenter.api.model.Message;
import com.primeton.wh.mcenter.impl.message.dao.ApplicationDAO;
import com.primeton.wh.mcenter.impl.message.service.IApplicationService;
import com.primeton.wh.mcenter.impl.message.service.IMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: YunTao.Li
 * @create: 2020-03-19 16:51
 * @description:
 **/
@Service
public class ApplicationServiceImpl implements IApplicationService {
    @Autowired
    private ApplicationDAO applicationDAO;

    @Autowired
    private IMessageService messageService;

    /**
     * 查询应用列表
     *
     * @param condition :
     * @return : com.baomidou.mybatisplus.core.metadata.IPage<com.primeton.wh.mcenter.impl.message.entity.Application>
     * @author : YunTao.Li
     * @date : 2020/3/19 2020/3/19
     */
    @Override
    public IPage<Application> getApplicationList(Map<String, Object> condition) throws Throwable {
        String msgTo = condition.get("msgTo").toString();

        // FIXME: 这里应该移除查询人的条件
        condition.remove("msgTo");

        IPage<Application> applicationIPage = this.applicationDAO.getApplicationList(condition);

//        msgTo = "10029417";
        //FIXME:为了满足业务将message的信息附着一下，这样做不好
        List<Application> applicationList = applicationIPage.getRecords();
        for (Application app : applicationList) {
            String appCode = app.getAppCode();

            // app's msg
            List<Message> msgList = messageService.getMessageListByApp(appCode, msgTo);
            List<Message> unreadMsgList = messageService.getUnreadMessageListByApp(appCode, msgTo);
            int count = msgList.size();
            int unreadCount = unreadMsgList.size();
            Message message = null;
            // nearest msg
            if (unreadCount > 0) {
                message = unreadMsgList.get(0);
            } else if(count > 0){
                message = msgList.get(0);
            } else {
                message = new Message();
            }
            String msgTitle = message.getMcTitle();
            Date msgTime = message.getMcSendtime();

            app.setNearestMsgTitle(msgTitle);
            app.setNearestMsgDate(msgTime);
            app.setUnreadMsgCount(unreadCount);
        }


        return applicationIPage;
    }

    /**
     * 插入
     *
     * @param record :
     * @return : java.util.Map<java.lang.String,java.lang.String>
     * @author : YunTao.Li
     * @date : 2020/3/19 2020/3/19
     */
    @Override
    public Map<String, String> addApplication(Application record) throws Throwable {
        Map<String, String> result = new HashMap<String, String>();
        result.put("code", "-1");
        try {
            applicationDAO.insertApplication(record);
        } catch (Throwable e) {
            e.printStackTrace();
            throw e;
        }

        result.put("code", "1");
        return result;
    }

    /**
     * 编辑
     *
     * @param record :
     * @return : java.util.Map<java.lang.String,java.lang.String>
     * @author : YunTao.Li
     * @date : 2020/3/19 2020/3/19
     */
    @Override
    public Map<String, String> editApplication(Application record) throws Throwable {
        Map<String, String> result = new HashMap<String, String>();
        result.put("code", "-1");
        try {
            applicationDAO.updateApplication(record);
        } catch (Throwable e) {
            e.printStackTrace();
            throw e;
        }

        result.put("code", "1");
        return result;
    }
}
